<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/27
 * Time: 10:19
 */

namespace Wechat\Controller;


class MessageboxController extends CommonController {

    /**
     * 留言
     */
    public function index() {
        $customerId = $this->customer["customer_id"];

        $messageMap["customer_id"] = $customerId;
        $messageMap["day"] = date("Y-m-d");
        $messagebox = D("Messagebox")->queryByMap($messageMap);

        $ownProduct = D("Product")->queryById($messagebox["product_id"]);

        $this->assign("customerId", $customerId);
        $this->assign("ownProduct", $ownProduct);
        $this->assign("info", $messagebox);
        $this->display("index");

    }

    /**
     * 提交留言
     */
    public function updateMessagebox() {

        $messagebox = D("Messagebox")->queryById(I("post.messageId"));

        $messagebox["customer_id"] = I("post.customerId");
        $messagebox["product_id"] = I("post.productId");
        $messagebox["day"] = date("Y-m-d");
        $messagebox["create_time"] = getCurrentDate();
        $messagebox["number"] = I("post.number");
        $messagebox["city"] = I("post.city");
        $messagebox["message_content"] = I("post.messageContent");
        $messagebox["status"] = C("MESSAGEBOX_STATUS.WAITING_REPLY");

        $messageboxId = $messagebox["messagebox_id"];
        if(!isNullOrEmpty($messageboxId)){
            D("Messagebox")->updateInfo($messagebox);
        }else{
            $messageboxId = D("Messagebox")->add($messagebox);
        }

        //推送预约信息到管理员
//        $notifyData["openid"] = array("otBeowu8bYi2HBRTMBWSIWzzkkbo","otBeowlAfkwtcoHunTHTDVQucZ_E","otBeowv0VZQORekZps57qRBc5d_0");
        //$notifyData["openid"] = "otBeowu8bYi2HBRTMBWSIWzzkkbo";
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',', $singleEmployee["job"]);
            if ($job[0] == "5" || $job[0] == "100") {//西钓客服
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }
        foreach ($openidArray as $openid) {
            $notifyData["openid"] = $openid;
            $notifyData["url"] = "http://xwkang.com/wechat/messagebox/viewnotify/id/" . $messageboxId;
            $notifyData["first"] = "收到患者留言，请通知相关销售。";
            $notifyData["keyword1"] = "产品：【" . I("post.productName") . "】。";
            $notifyData["keyword2"] = $messagebox["message_content"];
            $notifyData["keyword3"] = $messagebox["create_time"];
            $notifyData["remark"] = "电话：【" . $messagebox["number"] . "】，所在城市：【" . $messagebox["city"] . "】";
            finishedOrderNotify($notifyData);
        }
        $data["code"] = 200;

        echo json_encode($data);

    }

    public function viewNotify() {
        $messagebox = D("Messagebox")->queryById($_REQUEST["id"]);
        $ownProduct = D("Product")->queryById($messagebox["product_id"]);
        $customer = D("Customer")->queryById($messagebox["customer_id"]);
        $employee = D("Employee")->queryById($this->customer["customer_id"]);
        $job = explode(',', $employee["job"]);

        $this->assign("customer", $customer);
        $this->assign("job", $job);
        $this->assign("ownProduct", $ownProduct);
        $this->assign("messagebox", $messagebox);
        $this->display();

    }

    public function notify()
    {

        $messageId = $_REQUEST["messageId"];

        $messagebox = D("Messagebox")->queryById($messageId);
        $messagebox["status"] = C("MESSAGEBOX_STATUS.END");

        $customer = D("Customer")->queryById($_REQUEST["employeeId"]);
        $employee = D("Employee")->queryById($_REQUEST["employeeId"]);
        if($messagebox["notified"] != "")
            $messagebox["notified"] = $messagebox["notified"]."，";
        $messagebox["notified"] = $messagebox["notified"].$employee["employee_name"];

        //推送留言信息到销售
        $notifyData["openid"] = $customer["openid"];
        $notifyData["url"] = "http://xwkang.com/wechat/messagebox/viewnotify/id/" . $messageId;
        $notifyData["first"] = "收到患者留言，请您联系该患者。";
        $notifyData["keyword1"] = "产品：【" . $_REQUEST["productName"] . "】。";
        $notifyData["keyword2"] = $_REQUEST["messageContent"];
        $notifyData["keyword3"] = $_REQUEST["createTime"];
        $notifyData["remark"] = "微信昵称：". $_REQUEST["nickname"] ."，所在城市：【" . $_REQUEST["city"] . "】";
        if($_REQUEST["hospital"] != ""){
            $notifyData["remark"] = $notifyData["remark"]."，所属医院：".$_REQUEST["hospital"];
        }
        if($_REQUEST["doctor"] != ""){
            $notifyData["remark"] = $notifyData["remark"]."，主治医生：".$_REQUEST["doctor"];
        }
        $notifyData["remark"] = $notifyData["remark"]."，电话：".$_REQUEST["number"] . "（进入'详情'可直接拨打）";
        messageboxNotify($notifyData);
        D("Messagebox")->updateInfo($messagebox);

        $data["code"] = 200;

        echo json_encode($data);
    }
}